package com.worldcup.controller;

import com.google.common.collect.Lists;
import com.worldcup.common.CommonResult;
import com.worldcup.model.Country;
import com.worldcup.model.Game;
import com.worldcup.model.dto.GameDTO;
import com.worldcup.service.CountryService;
import com.worldcup.service.GameService;
import com.worldcup.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author duwan.zq
 * @date 2018/05/29
 */
@RestController
@RequestMapping("/api/java/game")
public class GameController {

    @Autowired
    private GameService gameService;

    @Autowired
    private CountryService countryService;

    @RequestMapping("/group/list")
    public CommonResult getGroupGame(String group, String city, String team, Integer limit, String zone){
        List<Game> gameList = gameService.listGameByGroup(group, city, team, limit);
        if(CollectionUtils.isEmpty(gameList)){
            return CommonResult.buildSuccess();
        }
        List<GameDTO> result = buildGameDTOList(gameList, zone);
        return CommonResult.buildSuccess(result);
    }

    @RequestMapping("/knock/list")
    public CommonResult getKnockGame(String type, String zone){
        List<Game> gameList = gameService.listGameByMatchType(type);
        if(CollectionUtils.isEmpty(gameList)){
            return CommonResult.buildSuccess();
        }
        List<GameDTO> result = buildGameDTOList(gameList, zone);
        return CommonResult.buildSuccess(result);
    }

    @RequestMapping("/getHotCity")
    public CommonResult getHotCity(){
        List<String> list = Lists.newArrayList("阿布扎比","艾因","沙迦","迪拜");
        return CommonResult.buildSuccess(list);
    }

    @RequestMapping("/getAllCity")
    public CommonResult getAllCity(){
        List<String> list = Lists.newArrayList("阿布扎比","艾因","沙迦","迪拜");
        return CommonResult.buildSuccess(list);
    }


    private List<GameDTO> buildGameDTOList(List<Game> gameList, String zone) {
        List<GameDTO> result = new ArrayList<>(gameList.size());
        for(Game game : gameList){
            GameDTO gameDTO = new GameDTO(game, countryService.getImageUrl(game.getHomeTeam()), countryService.getImageUrl(game.getAwayTeam()));
            if(!"china".equalsIgnoreCase(zone)){
                gameDTO.setTime(DateUtils.toUAETimeString(gameDTO.getTime()));
            }
            result.add(gameDTO);
        }
        return result;
    }
}
